package com.imcrl.piano.mvc.view
{
	import com.imcrl.piano.core.Assets;
	import com.imcrl.piano.core.KeyAssets;
	import com.imcrl.piano.mvc.model.vo.NoetsDefVO;
	import com.imcrl.piano.mvc.view.listItem.NoetsItemRender;
	import com.imcrl.piano.utils.SpriteUtils;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import org.osmf.events.TimeEvent;
	
	public class PianoView extends Sprite
	{
		
		public static var VIEW_PORT_RECT:Rectangle;
		public static const PADDING:int=343;
		public static const EFFECT_PADDING:int=143;
		
		public var startBtn:SimpleButton;
		private var keyboardMc:Sprite;
		
		private var effect:MovieClip;
		
		public var scoringView:ScoringView;
		
		private var _data:Vector.<NoetsDefVO>;
		
		private var noetsContainer:DisplayObjectContainer;
		
		private var timer:Timer;
		public function PianoView()
		{
			
			if(VIEW_PORT_RECT==null){
				VIEW_PORT_RECT=new Rectangle(0,0,700,120);
			}
			
			addEventListener(Event.ADDED_TO_STAGE,addStageHandler);
			addEventListener(Event.REMOVED_FROM_STAGE,removeStageHandler);
			
			
			buildCanvas();
		}
		
		protected function buildCanvas():void{
			
			var bg:Bitmap=new Assets.bgClass();
			
			this.addChild(bg);
			
			keyboardMc=new Assets.KeyboardClass();
			SpriteUtils.stopAll(keyboardMc);
			
			keyboardMc.y=bg.height+3;
			keyboardMc.x=(996-keyboardMc.width)>>1;
			this.addChild(keyboardMc);
			
			
			startBtn=new Assets.StartBtnClass();
			startBtn.x=30;
			startBtn.y=bg.height;
			this.addChild(startBtn);
			
			noetsContainer=SpriteUtils.createDisplayContainer();
			noetsContainer.scrollRect=VIEW_PORT_RECT;
			
			
			/**输助查看偏移坐标位置;
			 var g:Graphics=noetsContainer["graphics"];
			 g.beginFill(0x990000,.3);
			 g.drawRect(0,0,700,120);
			 */
			
			//把坐标偏到与背影同步;
			noetsContainer.x=150;
			noetsContainer.y=40;
			this.addChild(noetsContainer);
			
			effect=new Assets.EffectCircle();
			effect.visible = false
			effect.gotoAndStop(1);
			
			effect.x=noetsContainer.x+EFFECT_PADDING;
			effect.y=50;
			this.addChild(effect);
			
			
			scoringView=new ScoringView();
			scoringView.x=noetsContainer.x+60;
			scoringView.y=100;
			this.addChild(scoringView);
		}
		
		
		public function clear():void{
			var rect:Rectangle=noetsContainer.scrollRect;
			rect.x=0;
			//实际音符运行毫秒数;
			position=0;
			noetsContainer.scrollRect=rect;
			
			SpriteUtils.clear(noetsContainer);
		}
		
		
		public function start():void{
			if(timer==null){
				timer=new Timer(10);
				timer.addEventListener(TimerEvent.TIMER,timerTickHandler);
			}
			
			/** todo 更好的策略;
			 setTimeout(function():void{
			 new Assets.BgSound().play();
			 },570);
			 */
			timer.start();
		}
		
		
		public function stop():void{
			timer.stop();
		}
		
		public function restart():void{
			var rect:Rectangle=noetsContainer.scrollRect;
			rect.x=0;
			noetsContainer.scrollRect=rect;
			
			timer.reset();
			timer.start();
		}
		
		public var position:int;
		private function timerTickHandler(event:TimerEvent):void{
			var pos:int=timer.currentCount;
			
			var rect:Rectangle=noetsContainer.scrollRect;
			rect.x=pos;
			
			//实际音符运行毫秒数;
			position=(pos-PADDING+EFFECT_PADDING)*10;
			
			noetsContainer.scrollRect=rect;
			
			this.dispatchEvent(event.clone());
		}
		
		public function set data(value:Vector.<NoetsDefVO>):void{
			if(_data==value)return;
			
			_data=value;
			
			this.clear();
			
			var render:NoetsItemRender;
			for each(var item:NoetsDefVO in _data){
				render=new NoetsItemRender();
				render.data=item;
				render.x=PADDING+item.start/10;
				//-7刚好让它Y轴相反排序;
				render.y=Math.abs(item.node-7)*15;
				noetsContainer.addChild(render);
			}
		}
		
		
		public function down(node:int):void{
			effect.visible=true;
			effect.gotoAndPlay(1);
			effect.y=noetsContainer.y+Math.abs(node-7)*15;
			effect.addEventListener(Event.ENTER_FRAME,effectEnterFrameHandler);
			
			node--;
			var mc:MovieClip=	keyboardMc["btn"+node] as MovieClip;
			if(mc)mc.gotoAndStop(2);
		}
		
		public function up(node:int):void{
			node--;
			var mc:MovieClip=keyboardMc["btn"+node] as MovieClip;
			if(mc)mc.gotoAndStop(1);
		}
		
		private function effectEnterFrameHandler(event:Event):void{
			if(effect.currentFrame==effect.totalFrames){
				effect.gotoAndStop(1);
				effect.visible=false;
				effect.removeEventListener(Event.ENTER_FRAME,effectEnterFrameHandler);
			}
		}
		
		private function addStageHandler(event:Event):void{
			stage.addEventListener(Event.RESIZE,resizeHandler);
			resizeHandler(event);
		}
		private function removeStageHandler(event:Event):void{
			stage.removeEventListener(Event.RESIZE,resizeHandler);
		}
		
		private function resizeHandler(event:Event):void{
			this.x=(stage.stageWidth-width)>>1;
			this.y=(stage.stageHeight-height)>>1
		}
		
	}
}